package com.summer.web.controller;

import com.summer.pojo.Book;
import com.summer.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/userfindAllBook")
    public String findAllBook(Model model){
        List<Book> bookList = bookService.findAllBook();
        for (Book book : bookList) {
            System.out.println(book);
        }
        model.addAttribute("list",bookList);
        return "BookStore";
    }
    @RequestMapping("/adminfindAllBook")
    public String adminfindAllBook(Model model){
        List<Book> bookList = bookService.findAllBook();
        for (Book book : bookList) {
            System.out.println(book);
        }
        model.addAttribute("list",bookList);
        return "admin";
    }

    @RequestMapping("/adminAddBooks")
    public String adminAddBook(Book book){
        bookService.addBook(book);
        return "redirect:/book/adminfindAllBook";
    }
    @RequestMapping("/adminUpdateBook")
    public String adminupdateBook(Book book){
        bookService.adminupdateBookById(book);
        return "redirect:/book/adminfindAllBook";
    }
    @RequestMapping("/adminfindBookById")
    public String adminfindBookById(int id,Model model){
        Book book = bookService.findBookById(id);
        model.addAttribute("book",book);
        return "adminupdatebook";
    }
    @RequestMapping("/admindeleteBookById")
    public String admindeleteBookById(int bookid){
        bookService.deleteBookById(bookid);
        return "redirect:/book/adminfindAllBook";
    }

}
